package com.motu.vertx.module.utility.webhandler;

import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import com.motu.vertx.module.utility.webtool.BaseWebHandler;
import com.motu.vertx.module.utility.webtool.SearchCondition;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.WebClient;
import io.vertx.sqlclient.Pool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ErrorcodeHandler extends BaseWebHandler{
	private static final Logger logger = LoggerFactory.getLogger(ErrorcodeHandler.class);
	public static final String tableName = "errorcode";
	private static Pool jdbcClientServer = null;

	public static void init(Pool jdbcServer){
		jdbcClientServer = jdbcServer;
	}
	public static void list(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getTableList(routingContext, jdbcClientServer,tableName);
	}
	

	public static void search(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		List<SearchCondition> scList = new ArrayList<SearchCondition>();
		if (json.containsKey("stime")) {
			json.put("stime", TimeUtils.getTimeStrIntValue(json.getString("stime")));
		}
		if (json.containsKey("etime")) {
			json.put("etime", TimeUtils.getTimeStrIntValue(json.getString("etime")));
		}
		scList.add(new SearchCondition("stime", ">=", new String[]{"addtime"}, null));
		scList.add(new SearchCondition("etime", "<=", new String[]{"addtime"}, null));
		scList.add(new SearchCondition("errcode", "=", null, null));
		scList.add(new SearchCondition("cmd", "like", null, null));
		scList.add(new SearchCondition("userid", "=", null, null));
		
		searchFromTable(routingContext, jdbcClientServer, json, tableName, scList);

	}


	/**
	 * 统计
	 * @param routingContext
	 * @param json
	 * @param adminName
	 */
	public static void count(RoutingContext routingContext, JsonObject json,String adminName,WebClient client) {
		HttpServerResponse response = routingContext.response();
		List<SearchCondition> scList = new ArrayList<>();
		if (json.containsKey("stime")) {
			json.put("stime", TimeUtils.getTimeStrIntValue(json.getString("stime")));
		}
		if (json.containsKey("etime")) {
			json.put("etime", TimeUtils.getTimeStrIntValue(json.getString("etime")));
		}
		scList.add(new SearchCondition("stime", ">=", new String[] {"addtime"}, null));
		scList.add(new SearchCondition("etime", "<", new String[] {"addtime"}, null));
		scList.add(new SearchCondition("errcode", "=", null, null));
		scList.add(new SearchCondition("cmd", "like", null, null));
		JsonArray params = new JsonArray();
		String str_where = getSearchString(scList, json, params);
		String sql = "SELECT cmd,errcode,COUNT(1) as num FROM " + tableName  + " WHERE 1=1 " + str_where;
		sql += " GROUP BY cmd,errcode ORDER BY num DESC ";
		MysqlTool.queryJsonArrayFromDb(jdbcClientServer, sql, params, handler->{
			if (handler.succeeded()){
				JsonObject result = new JsonObject();
				result.put("rows", handler.result());
				sendHttpResponse(response, result);
			} else{
				logger.error(handler.cause().getMessage());
				sendHttpResponseError(response,  handler.cause().getMessage());
			}
		});
	}

	

}
